package com.example.aigc_education.integration.response.workflows.v2;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@Slf4j
@JsonDeserialize(using = CozeGenQuizzesResponse.CozeGenQuizzesResponseDeserializer.class)
public class CozeGenQuizzesResponse implements Serializable {

    @JsonProperty("Output")
    private String output;

    private List<QuizzesData> questions;

    @Data
    public static final class QuizzesData implements Serializable{
        private List<String> answer;

        private List<String> options;

        private String question;

        private String explanation;

        @JsonProperty("question_type")
        private String questionType;
    }

    /**
     * 自定义反序列化器，用于处理整个响应对象
     * 直接将嵌套的JSON解析为questions列表
     */
    public static final class CozeGenQuizzesResponseDeserializer extends JsonDeserializer<CozeGenQuizzesResponse> {
        @Override
        public CozeGenQuizzesResponse deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            ObjectMapper mapper = (ObjectMapper) p.getCodec();
            JsonNode rootNode = p.readValueAsTree();
            
            CozeGenQuizzesResponse response = new CozeGenQuizzesResponse();
            
            // 处理直接包含questions数组的情况
            JsonNode questionsNode = rootNode.path("questions");
            if (!questionsNode.isMissingNode() && questionsNode.isArray()) {
                response.setQuestions(parseQuestionsArray(mapper, questionsNode));
                return response;
            }
            
            // 处理包含Output字段的情况
            JsonNode outputNode = rootNode.path("Output");
            if (!outputNode.isMissingNode()) {
                String outputText = outputNode.asText();
                response.setOutput(outputText);
                
                try {
                    // 尝试解析Output字段中的JSON
                    JsonNode outputJsonNode = mapper.readTree(outputText);
                    
                    // 检查是否包含questions数组
                    questionsNode = outputJsonNode.path("questions");
                    if (!questionsNode.isMissingNode() && questionsNode.isArray()) {
                        response.setQuestions(parseQuestionsArray(mapper, questionsNode));
                        return response;
                    }
                } catch (Exception e) {
                    log.warn("解析Output字段中的JSON失败: {}", e.getMessage());
                }
            }
            
            // 如果没有找到questions数组，返回空列表
            if (response.getQuestions() == null) {
                response.setQuestions(new ArrayList<>());
            }
            
            return response;
        }
        
        /**
         * 解析questions数组节点为QuizzesData列表
         */
        private List<QuizzesData> parseQuestionsArray(ObjectMapper mapper, JsonNode questionsNode) throws JsonProcessingException {
            List<QuizzesData> questionsList = new ArrayList<>();
            
            for (JsonNode questionNode : questionsNode) {
                QuizzesData quizData = mapper.treeToValue(questionNode, QuizzesData.class);
                questionsList.add(quizData);
            }
            
            return questionsList;
        }
    }
}
